
package com.globant;

import java.util.ArrayList;

import java.util.List;


import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.globant.profy.model.User;

public class UserDAOImplementation extends HibernateDaoSupport implements
		UserDao {


	public UserDAOImplementation() {
	}


	public void createUser(User user) {

		getHibernateTemplate().save(user);
	}

	public void updateUser(User user) {
		getHibernateTemplate().update(user);
	}

	public void deleteUser(User user) {
		getHibernateTemplate().delete(user);

	}


	@SuppressWarnings("unchecked")
	public List<User> getAllUsers() {
		List<User> allUsers = getHibernateTemplate().loadAll(User.class);
		return allUsers;
	}

	public User getUserByEmail(final String email) {
		return (User) getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) {
				Query query = getSession().getNamedQuery("User.byEmail");
				query.setString("email", email);
				return (User) query.uniqueResult();
			}
		});
	}
	public List<User> getUserByCriteria(final String info) {
		List<User> users=new ArrayList<User>();
		Query query = getSession().createQuery("FROM User where company= :info");
		query.setString("info", info);
		users.addAll(query.list());
		query = getSession().createQuery("FROM User c where c.name= :info");
		query.setString("info", info);
		users.addAll(query.list());
		query = getSession().createQuery("FROM User where experience=:info");
		query.setString("info", info);
		users.addAll(query.list());
		return users;


		}


  }